package com.ym.student.system.controller;

import com.ym.student.system.entity.SysUserRole;
import com.ym.student.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-roles")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    // 获取所有用户角色关系
    @GetMapping
    public List<SysUserRole> getAllUserRoles() {
        return sysUserRoleService.list();
    }

    // 根据ID获取单个记录
    @GetMapping("/{id}")
    public SysUserRole getUserRoleById(@PathVariable Integer id) {
        return sysUserRoleService.getById(id);
    }

    // 添加用户角色关系
    @PostMapping
    public boolean addUserRole(@RequestBody SysUserRole sysUserRole) {
        return sysUserRoleService.save(sysUserRole);
    }

    // 更新用户角色关系
    @PutMapping("/{id}")
    public boolean updateUserRole(@PathVariable Integer id, @RequestBody SysUserRole sysUserRole) {
        sysUserRole.setId(id); // 确保更新的是指定ID的记录
        return sysUserRoleService.updateById(sysUserRole);
    }

    // 删除用户角色关系
    @DeleteMapping("/{id}")
    public boolean deleteUserRole(@PathVariable Integer id) {
        return sysUserRoleService.removeById(id);
    }
}
